//
//  BankSelectionViewController.m
//  NiuBanJin
//
//  Created by Billy on 15/10/23.
//  Copyright © 2015年 zzjr. All rights reserved.
//

#import "BankSelectionViewController.h"

@interface BankSelectionViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,copy)passValueBlock bankBlock;
@property (nonatomic,assign) int  pageNumber;

@property (nonatomic, strong) NSMutableArray *mutableArray_allBanks;

@property (nonatomic, strong) UITableView *tableView;

@end

@implementation BankSelectionViewController

- (instancetype)initWithBlock:(passValueBlock) bankBlock{
    
    self = [super init];
    if (self) {
        
        self.bankBlock = bankBlock;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self.navigationItem setTitle:@"选择银行"];
    self.pageNumber = 0;
    [self.view addSubview:self.tableView];
}

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    
    [self getBankDataFromServer];
}



#pragma mark - TableView datasource && delegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}


- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return [self.mutableArray_allBanks count];
}

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *identifer = @"cell" ;
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifer];
    
    if (!cell) {
        
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifer];
    }
    
    [cell.textLabel setText:self.mutableArray_allBanks[indexPath.row]];
    
    return  cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    self.bankBlock (self.mutableArray_allBanks[indexPath.row],@"0"); //替换成真实数据
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    return 55.0;
}


#pragma mark - data prepatioin

- (void) getBankDataFromServer {
    
    NSDictionary *dic_params = @{@"pageNum":[NSString stringWithFormat:@"%d",self.pageNumber]};
    
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [[NetworkUtil sharedNetworkUtil] zzjrBankListByParmeteres:dic_params transferResultUsingBlock:^(id parsedData, NSError *error) {
        
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        
        if (!error) {
            
            NSDictionary *dic_result = parsedData;
           
            if ([dic_result[@"status"] integerValue] == 0) {
                
                for (NSDictionary *dic_element in dic_result[@"result"]) {
                    
                    [self.mutableArray_allBanks addObject:dic_element[@"name"]];
                    
                }
            }
            [self.tableView reloadData];
        }else {
            
            [UsefulUtil showAlertView:error.description];
        }
    }];
}

#pragma mark - setter

- (NSMutableArray *)mutableArray_allBanks {
    
    if (!_mutableArray_allBanks) {
        
        _mutableArray_allBanks = [NSMutableArray array];
    }
    
    return _mutableArray_allBanks;
}

- (UITableView *)tableView {
    
    if (!_tableView) {
        
        _tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
        
        [_tableView setAutoresizingMask:UIViewAutoresizingFlexibleHeight];

        [_tableView setBackgroundColor:BASECOLOR];
        [_tableView setDelegate:self];
        [_tableView setDataSource:self];
        
        UIView *view = [[UIView alloc] initWithFrame:CGRectZero];
        [_tableView setTableFooterView:view];
    }
    
    return  _tableView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
  
    
}

@end
